package com.whmnx.chitchat.main;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.tencent.qcloud.tim.uikit.TUIKit;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.CommonDialog;
import com.whmnx.chitchat.view.CommonDialog2;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/7/20 0020 15:40
 * version: 1.0
 *
 * @author Administrator
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;

    String host = "";

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.login_activity);
        setHead_title(View.GONE);
    }

    @Override
    protected void loadData() {

    }



    private void login() {
        if (!UItils.isPhoneNumber(etPhone.getText().toString().trim())) {
            UItils.showToastSafe("请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(etPassword.getText().toString().trim())) {
            UItils.showToastSafe("请输入密码");
            return;
        }

        Map<String, Object> params = new HashMap<>();
        params.put("Mobile", etPhone.getText().toString().trim());
        params.put("Pwd", etPassword.getText().toString().trim());
        HttpClient.get(LoginActivity.this, Constants.GETUSERLOGIN, params, new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
                SPUtil.put(Constants.PHONE, result.getUser().getPhone());
                SPUtil.put(Constants.HEAD_IMG, result.getUser().getHeadImg());
                SPUtil.put(Constants.NICKNAME, result.getUser().getUserNick());
                SPUtil.put(Constants.USERID, result.getUser().getUserId());
                SPUtil.put(Constants.DEP, result.getUser().getOrganizeName());
                SPUtil.put(Constants.COM, result.getUser().getDepartmentName());
                SPUtil.put(Constants.TOKEN, result.Token);
                UItils.showToastSafe("登录成功");
                SPUtil.put(Constants.WELCOME, true);
                SPUtil.put(Constants.LOGIN, true);
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                //   initVideoCallData();
                finish();
                // getIMGenSig(result.getSig(), result.getUser().getPhone());
            }
        });
    }

    private void getIMGenSig(String sig, String mobile) {
        TUIKit.login(mobile, sig, new IUIKitCallBack() {
            @Override
            public void onSuccess(Object data) {
                //UItils.showToastSafe("登录成功");
                //  SPUtil.put(Constants.WELCOME, true);
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                // SPUtil.put(Constants.LOGIN, true);
                //   initVideoCallData();
                finish();
            }

            @Override
            public void onError(String module, int errCode, String errMsg) {
                //错误码 code 和错误描述 desc，可用于定位请求失败原因
                //错误码 code 列表请参见错误码表
                //登录正在执行中，例如，第一次 login 或 autoLogin 操作在回调前，后续的 login 或 autoLogin 操作会返回该错误码。7501
                Log.d("hhh", "login failed. code: " + errCode + " errmsg: " + errMsg);
                //    Utils.showToastSafe("login failed. code: " + errCode + " -------errmsg: " + errMsg);
                if (errCode == 7501) {
                    TUIKit.login(mobile, sig, new IUIKitCallBack() {
                        @Override
                        public void onSuccess(Object data) {
                            SPUtil.put(Constants.WELCOME, true);
                            SPUtil.put(Constants.LOGIN, true);
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        }

                        @Override
                        public void onError(String module, int errCode, String errMsg) {
                            //错误码 code 和错误描述 desc，可用于定位请求失败原因
                            //错误码 code 列表请参见错误码表
                            Log.d("", "login failed. code: " + errCode + " errmsg: " + errMsg);
                            //   Utils.showToastSafe("login failed. code: " + errCode + " -------errmsg: " + errMsg);
                        }
                    });
                }
            }
        });
    }



    @OnClick({R.id.tv_forget_password, R.id.sb_login, R.id.tv_registered})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_forget_password:
              UItils.showToastSafe("请联系管理员");
                break;
            case R.id.sb_login:
                login();
                break;
            case R.id.tv_registered:
                String s2 = "";
                //"http://192.168.0.18:8019/";
                new CommonDialog2(this, R.style.dialog, s2, (dialog, address,pos,confirm) -> {
                    if (confirm) {
                        Map<String, Object> params = new HashMap<>();
                        if (TextUtils.isEmpty(pos)){
                             host = "http://" + address  + "/";
                        }else {
                             host = "http://" + address + ":" + pos + "/";
                        }
                        String url = host + "AppApi/GetTestConnection";
                        HttpClient.get(LoginActivity.this, url, params,new CallBack<LoginBean>() {
                            @Override
                            public void onSuccess(LoginBean result) {
                                 SPUtil.put(Constants.HOSE, host);
                            }
                        });
                    }
                    dialog.dismiss();
                }).show();
                break;
            default:
        }
    }
}
